package com.css.fxfzdzzh.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Dbj
 * @date 2023年03月14日  17:47
 */
public class ObjUtils {
    public static void main(String[] args) {
        String str = "helloWordTestHe";
        System.out.println(underline(str));
        String str1 = "hello_Word_tESt_he";
        System.out.println(hump(str1));
    }
    /**
     * @author Howe
     * @Description 将驼峰转为下划线
     * @param str
     * @return java.lang.Stringhump
     */
    public static String underline(String str) {
        Pattern compile = Pattern.compile("[A-Z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            matcher.appendReplacement(sb,  "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * @author Howe
     * @Description 将下划线转为驼峰
     * @param str
     * @return java.lang.String
     * @Date   2022/4/22 13:12
     * @since  1.0.0
     */
    public static String hump(String str) {
        str = str.toLowerCase();
        Pattern compile = Pattern.compile("_[a-z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            matcher.appendReplacement(sb,  matcher.group(0).toUpperCase().replace("_",""));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
